<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "Client.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "Client.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Clients.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Users.php";

class Admin_ClientController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $modelClient;
    private $modelUser;
    
    /////////////////////
    // O V E R I D E S //
    ///////////////////// 
    public function init() {
        /* Initialize action controller here */
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        $this->modelClient = new End_Models_Clients();
        $this->modelUser = new End_Models_Users();

        //OTHER 
        $this->currentUser = $this->getInforUser();

        //VIEW 
        $this->view->currentUser = $this->currentUser;
        /* Initialize action controller here */
    }
    
    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }
    
    ////////////////////
    // A C T I O N S  //
    //////////////////// 
    public function addAction() {
        //Create form enter info for new Client
        $form = new Application_Form_Client();

        //Set label for the button
        $form->submit->setLabel('Add');

        //Set client view page form  = $form
        $this->view->form = $form;
        $created_date = date('Y-m-d H:i:s');
        //If can get client request 
        if ($this->getRequest()->isPost()) {
            //Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {   //If data is valid 
                //Get data one by one
                $name = $form->getValue('name');
                $email = $form->getValue('email');
                $telephone = $form->getValue('telephone');
                $address = $form->getValue('address');

                //Create new Page object from data
                $client = new Client(null, $name, $email, $telephone, $address, null);

                //Use insert function from Client model
                $this->modelClient->_insert($client);

                //Move to index page
                $this->_helper->redirector('index');
            } else {
                //Return form
                $form->populate($formData);
            }
        }
    }

    public function deleteAction() {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $client_id = (int) $this->getRequest()->getPost('client_id');
                $clients = new End_Models_Clients();
                $clients->_delete($client_id);
            }
            $this->_helper->redirector('index');
        } else {
            $client_id = $this->_getParam('client_id', 0);
            $this->view->Clients = Utility_Converter::objectToArray($this->modelClient->_find($client_id));
        }
    }

    public function editAction() {
        $form = new Application_Form_Client();
        $form->submit->setLabel('Save');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {

                $client_id = (int) $form->getValue('client_id');
                $name = $form->getValue('name');
                $telephone = $form->getValue('telephone');
                $email = $form->getValue('email');
                $address = $form->getValue('address');
                $create_date = $form->getValue('create_date');

                $client = new Client(null, $name, $telephone, $email, $address, $created_date); //dua thong tin vao doi tuong
                //tao doi tuong model voi cac phuong thuc xu li doi tuong o tren
                $this->modelClient->_update($client);        //xu li doi tuong voi phuong thuc cua model
                $this->_helper->redirector('index');    //chuyen den trang index
            } else {
                $form->populate($formData);
            }
        } else {
            $client_id = $this->_getParam('client_id', 0);
            if ($client_id > 0) {
                $form->populate(Utility_Converter::objectToArray($this->modelClient->_find($client_id)));
            }
        }
    }

    public function indexAction() {
        $client = $this->modelClient->fetchAll();
        $this->view->Client = $client;
    }

}

